package com.itbaizhan.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.ruiji.common.Result;
import com.itbaizhan.ruiji.dto.DishDto;
import com.itbaizhan.ruiji.pojo.Category;
import com.itbaizhan.ruiji.pojo.Dish;
import com.itbaizhan.ruiji.pojo.DishFlavor;
import com.itbaizhan.ruiji.service.CategorySevice;
import com.itbaizhan.ruiji.service.DishFlavorService;
import com.itbaizhan.ruiji.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategorySevice categorySevice;
    @PostMapping
    public Result<String> save(@RequestBody DishDto dishDto){
        log.info("保存菜品信息：{}",dishDto);
        dishService.savewithflavor(dishDto);
        return Result.success("新增菜品成功");
    }
    @GetMapping("/page")
    public Result<Page> page(int page,int pageSize,String name){
        log.info("分页查询：{}",page,pageSize);
        Page<Dish> pageinfo = new Page<>(page,pageSize);
        Page<DishDto> pageinfoDto = new Page<>();
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(name!=null,Dish::getName,name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(pageinfo,queryWrapper);
        BeanUtils.copyProperties(pageinfo,pageinfoDto,"records");
        List<Dish> records = pageinfo.getRecords();
        List<DishDto> list = records.stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categorySevice.getById(categoryId);
           if(category!=null){
               String categoryName = category.getName();
               dishDto.setCategoryName(categoryName);
           }
            return dishDto;
        }).collect(Collectors.toList());
        pageinfoDto.setRecords(list);
        return Result.success(pageinfoDto);
    }

    @GetMapping("/{id}")
    public Result<DishDto> get(@PathVariable Long id){
        log.info("查询id：{}",id);
        DishDto dishDto = dishService.getByidWithFlavor(id);
        return Result.success(dishDto);
    }


    //更新修改菜品
    @PutMapping
    public Result<String> update(@RequestBody DishDto dishDto){
        log.info("保存菜品信息：{}",dishDto);
        dishService.updatewithflavor(dishDto);
        return Result.success("修改菜品成功");
    }

//    @PostMapping("/status")
//    public Result<Dish> status(int status,long id){
//        log.info("修改菜品状态：{}",status);
//        dishService
//    }

//    @GetMapping("/list")
//    public Result<List<Dish>> list(Dish  dish){
//        log.info("查询菜品：{}",dish);
//        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
//        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
//        queryWrapper.eq(Dish::getStatus,1);
//        List<Dish> list = dishService.list(queryWrapper);
//        return Result.success(list);
//    }

    @GetMapping("/list")
    public Result<List<DishDto>> list(Dish  dish){
        log.info("查询菜品：{}",dish);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        queryWrapper.eq(Dish::getStatus,1);
        List<Dish> list = dishService.list(queryWrapper);
        List<DishDto> dishDtoList=list.stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long dishid = item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId,dishid);
            lambdaQueryWrapper.orderByAsc(DishFlavor::getUpdateTime);
            List<DishFlavor> flavorList = dishFlavorService.list(lambdaQueryWrapper);
            dishDto.setFlavors(flavorList);
            return dishDto;
        }).collect(Collectors.toList());
        return Result.success(dishDtoList);
    }


}
